﻿package hibernate.xml44.test;


import hibernate.xml44.pojo.UserWebsite;
import hibernate.xml44.pojo.WebUser;
import hibernate.xml44.util.HibernateHandler;

import java.util.Set;

import org.hibernate.Hibernate;
import org.hibernate.Session;

public class Lazy {
	public static void main(String[] args) {
//		lazyResolve();
	}

	public static void lazyResolve() {
		Session session = HibernateHandler.getSession();
		session.beginTransaction();
		WebUser webuser = (WebUser) session.get(WebUser.class, 110);
		
		/*解决懒加载方案1.在事务提交前Session关闭前明确初始化*/
		Hibernate.initialize(webuser.getUserWebsite());
		
		session.getTransaction().commit();
		session.close();
		
		/*session关闭后取出自身属性*/
		String name = webuser.getName();
		System.out.println("name="+name);
		/*session关闭后取出和本对象关联的另一个对象的属性*/
		Set<UserWebsite> set = webuser.getUserWebsite();
		for(UserWebsite uw:set){
			System.out.println(uw.getId());
		}
	}
}
